package cn.pubinfo.commentapi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @author kuancz
 * @date 2018/7/26
 */
public class DateUtil {
    private static final Pattern DATE_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");

    public static Long parseTimestamp(String dateStr) {
        if (dateStr == null){
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        if (DATE_PATTERN.matcher(dateStr).matches()) {
            try {
                String str = dateStr + " 00:00:00.000";
                Date date = dateFormat.parse(str);
                return -1 * date.getTime() / 1000;
            } catch (ParseException e) {
                return null;
            }
        }
        return null;
    }
}
